﻿namespace iFinance.SqoDAL
{
    using System.Collections.Generic;
    using System.Linq;
    using iFinance.Entity;
    using iFinance.IDAL;

    /// <summary>
    /// 支出记录数据库操作类
    /// </summary>
    public class OutgoingsDAO : IOutgoingsDAO
    {
        /// <summary>
        /// 添加支出项，并返回添加后生成的唯一编号，如果失败返回-1
        /// </summary>
        /// <param name="outgoings">要添加的支出项</param>
        /// <returns>成功返回添加后生成的唯一编号，失败返回-1</returns>
        public int AddOutgoings(Outgoings outgoings)
        {
            bool result = IndividualSqoHelper.Instance.Insert(outgoings);
            return result == true ? outgoings.OID : -1;
        }

        /// <summary>
        /// 添加支出明细项
        /// </summary>
        /// <param name="outgoingsDetail">要添加的支出明细项</param>
        /// <returns>是否成功</returns>
        public bool AddOutgoingsDetail(OutgoingsDetail outgoingsDetail)
        {
            return IndividualSqoHelper.Instance.Insert(outgoingsDetail);
        }

        /// <summary>
        /// 查询支出明细
        /// </summary>
        /// <param name="outgoingsID">支出项的ID</param>
        /// <returns></returns>
        public List<OutgoingsDetail> QueryOutgoingsDetail(int outgoingsID)
        {
            return (from OutgoingsDetail outgoingsDetail in IndividualSqoHelper.Instance.DBContext
                    where outgoingsDetail.OutgoingsID.Equals(outgoingsID)
                    select outgoingsDetail).ToList();
        }

        /// <summary>
        /// 查询当前登录用户的所有支出项
        /// </summary>
        /// <returns></returns>
        public List<Outgoings> QueryAllOutgoings()
        {
            return (from Outgoings outgoings in IndividualSqoHelper.Instance.DBContext
                    select outgoings).ToList();
        }

        /// <summary>
        /// 删除支出项以及其明细
        /// </summary>
        /// <param name="outgoingsID">要删除的支出项编号</param>
        /// <returns></returns>
        public bool DeleteOutgoings(int outgoingsID)
        {
            var item = (from Outgoings outgoings in IndividualSqoHelper.Instance.DBContext
                        where outgoings.ID == outgoingsID
                        select outgoings).FirstOrDefault();
            if (item != null)
            {
                bool result = IndividualSqoHelper.Instance.Delete(item);
                if (result)
                {
                    //删除支出明细项
                    var details = QueryOutgoingsDetail(outgoingsID);
                    foreach (OutgoingsDetail detail in details)
                    {
                        IndividualSqoHelper.Instance.Delete(detail);
                    }
                }
                return result;
            }
            return false;
        }

        /// <summary>
        /// 更新支出项
        /// </summary>
        /// <param name="outgoings"></param>
        /// <returns></returns>
        public bool UpdateOutgoings(Outgoings outgoings)
        {
            return IndividualSqoHelper.Instance.Update(outgoings);
        }

        /// <summary>
        /// 更新支出明细项
        /// </summary>
        /// <param name="outgoingsDetail"></param>
        /// <returns></returns>
        public bool UpdateOutgoingsDetail(OutgoingsDetail outgoingsDetail)
        {
            return IndividualSqoHelper.Instance.Update(outgoingsDetail);
        }
    }
}
